home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Oh!X 2001 Spring
/
Oh!X 2001 Spring Special CD-ROM (Japan).7z
/
Oh!X 2001 Spring Special CD-ROM (Japan) (Track 1).bin
/
PUZZLE
/
puz01
/
ms1.c
< prev
next >
Wrap
C/C++ Source or Header
|
2000-02-20
|
2KB
|
105 lines
/*
* ms1.c : マジックスター全解探索
*
* Copyright (C) 2000 by Makoto Hiroi
*
*/
/*
パズルの内容
0
/ \
1------2------3------4
\ / \ /
5 6
/ \ / \
7------8------9------10
\ /
11
1 - 12 の数字を入れて直線上にある4つの数字の和が
全て 26 になるようにする
直線
0-2-5-7, 0-3-6-10, 7-8-9-10
1-2-3-4, 1-5-8-11, 4-6-9-11
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
#define N 12
#define LINE 6
const char line[LINE][4] = {
0, 2, 5, 7, 0, 3, 6, 10, 7, 8, 9, 10,
1, 2, 3, 4, 1, 5, 8, 11, 4, 6, 9, 11
};
char star[N];
char use_number[N + 1];
int count = 0;
/* 出力 */
void print_star( void )
{
int i;
for( i = 0; i < N; i++ ){
printf("%2d ", star[i] );
}
printf("\n");
}
/* 星の検査 */
int check_star( void )
{
int i;
for( i = 0; i < LINE; i++ ){
int j, n;
for( j = n = 0; j < 4; j++ ){
n += star[ line[i][j] ];
}
if( n != 26 ) return FALSE;
}
return TRUE;
}
/* 単純な生成検定法 */
void search_star( int n )
{
int i;
if( n == N && check_star() ){
count++;
print_star();
} else {
for( i = 1; i <= N; i++ ){
if( !use_number[i] ){
use_number[i] = TRUE;
star[n] = i;
search_star( n + 1 );
use_number[i] = FALSE;
}
}
}
}
int main()
{
int start, end;
memset( use_number, 0, N + 1 ); /* 初期化 */
printf("時間がかかるので1から始まる順列のみをチェックする\n");
use_number[1] = TRUE;
star[0] = 1;
start = clock();
search_star( 1 );
end = clock();
printf("総数 %d 個, 時間 %d\n", count, end - start );
return 0;
}
/* end of file */